import dayjs from "dayjs";
import dayjsZH from "dayjs/locale/zh";
import dayjsRelativeTime from "dayjs/plugin/relativeTime";
import dayjsLocalizedFormat from "dayjs/plugin/localizedFormat";
// import dayjsUtc from "dayjs/plugin/utc";

import currency from "currency.js";

dayjs.locale(dayjsZH);
dayjs.extend(dayjsRelativeTime);
dayjs.extend(dayjsLocalizedFormat);
// dayjs.extend(dayjsUtc);

// set function parseTime,formatTime to filter
// export { parseTime } from "@/utils";

export function autoFormatFileSize(filesize: number) {
  let count = 0;
  while (filesize > 1024) {
    filesize /= 1024;
    count++;
  }
  const unit = ["", "K", "M", "G", "T", "P", "E", "Z", "Y"];
  return `${filesize.toFixed(1)}${unit[count]}B`;
}

function pluralize(time, label) {
  if (time === 1) {
    return time + label;
  }
  return time + label + "s";
}
/**
 * 格式化时间（显示距当前时间相隔的时间）
 * @param time
 * @returns
 */
export function timeAgo(time) {
  const between = Date.now() / 1000 - Number(time);
  if (between < 3600) {
    return pluralize(~~(between / 60), " minute");
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), " hour");
  } else {
    return pluralize(~~(between / 86400), " day");
  }
}
/**
 * 格式化日期
 * @param time
 * @returns
 */
export function formatDate(time) {
  if (time) {
    return dayjs(time).format("L");
  } else {
    return "";
  }
}
export function time(time) {
  if (time) {
    return dayjs(time).format("HH:mm");
  } else {
    return "";
  }
}
/**
 * 格式化时间
 * @param time
 * @param format 采用 dayjs 的格式化规则
 * @returns
 */
export function formatTime(time, format = "YYYY-M-D HH:mm:ss") {
  if (time) {
    return dayjs(time).format(format);
  } else {
    return "";
  }
}
/**
 * 格式化时间
 * @param time
 * @param format 采用 dayjs 的格式化规则
 * @param fromNowHours 相对于多久的时间显示相对结果
 * @returns
 */
export function formatTime2(time, format = "YYYY-M-D HH:mm", fromNowHours = 12) {
  if (time) {
    const oldTime = dayjs(time);
    if (dayjs().diff(oldTime, "hours", true) >= fromNowHours) {
      return oldTime.format(format);
    } else {
      return oldTime.fromNow();
    }
  } else {
    return "";
  }
}

/**
 * 传入时间，获得年龄
 * @param time
 * @returns
 */
export function AGE(time) {
  if (time) {
    return dayjs().diff(time.toString(), "y");
  } else {
    return "未填写";
  }
}

/**
 * 数字 格式化
 */
export function numberFormatter(num, digits) {
  const si = [
    { value: 1e18, symbol: "E" },
    { value: 1e15, symbol: "P" },
    { value: 1e12, symbol: "T" },
    { value: 1e9, symbol: "G" },
    { value: 1e6, symbol: "M" },
    { value: 1e3, symbol: "k" },
  ];

  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value + 0.1).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, "$1") + si[i].symbol;
    }
  }
  return num.toString();
}

/**
 * 数字千分位格式化
 * @param num
 */
export function toThousandFilter(num) {
  return (Number(num) || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ","));
}

export function MONEY(money) {
  return currency(money);
}

export function RMB(money) {
  return currency(money, {
    symbol: "￥",
    precision: 2,
  }).format();
}

export function phoneMask(phone) {
  return phone.substr(0, 3) + " **** " + phone.substr(-4);
}

export { formatPhone } from "./cityPhoneCode";
